<?php


namespace App\Services;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

/**
 * 阿里云发送短信
 * Class AliSms
 * @package App\Services
 */
class AliSms
{
    static $sginName = '联航科技';

    static $temp = [
        1   => 'SMS_200845032' //验证码 ${code}
    ];

    public static function sendCode($mobile, int $temp, array $data)
    {
        AlibabaCloud::accessKeyClient(config('services.ali_access.key'),config('services.ali_access.secret'))
                        ->regionId('cn-hangzhou')
                        ->asDefaultClient();
        try {
            $result = AlibabaCloud::rpc()
                            ->host('dysmsapi.aliyuncs.com')
                            ->product('Dysmsapi')
                            // ->scheme('https') // https | http
                            ->version('2017-05-25')
                            ->action('SendSms')
                            ->method('POST')
                            ->options([
                                'query' => [
                                    'RegionId' => "cn-hangzhou",
                                    'PhoneNumbers' => $mobile,
                                    'SignName' => "联航科技",
                                    'TemplateCode' => self::$temp[$temp],
                                    'TemplateParam' => json_encode($data),
                                ],
                            ])
                            ->request();
            return $result;
        }catch (ClientException $exception){

            return [
                'Code'      => $exception->getErrorCode(),
                'Message'   => $exception->getErrorMessage(),
            ];
        }catch (ServerException $exception){

            return [
                'Code'      => $exception->getErrorCode(),
                'Message'   => $exception->getErrorMessage(),
            ];
        }
    }

}